<?xml version="1.0" encoding="utf-8"?>
<Content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:blueonionsoftware-com:bloget:runtime:data">
  <text>Suggested Event/Delegate naming conventions
8/17/2005 by D. Ei

1.  Choose the name of your event {ChosenName}.  It should be a verb describing the action associated with the event (e.g. â€˜clickâ€™, â€˜paintâ€™, etc.). Create an event in the Publisher class and name it 'ChosenName'. For example:

      public event LogicChangedEventHandler LogicChanged;

This is what Microsoft generally does.

2.  If you have data to return to your callback methods when your event fires, derive a class from System.EventArgs to hold this info. This class should be named â€˜ChosenNameEventArgs'. For example:
   
    public class LogicChangedEventArgs : System.EventArgs { }

If you donâ€™t have data to return, use System.EventArgs in your delegate signature, and pass System.EventArgs.Empty when you fire your event.

This is what Microsoft recommends.

3.  Define your delegate signature. Name it â€˜ChosenNameEventHandlerâ€™. The delegateâ€™s first argument should be the object that fired the event, the second argument should be derived from System.EventArgs. For example:

      public delegate void LogicChangedEventHandler(object source, LogicChangedEventArgs theChange);

This is what Microsoft generally does.

4.  Create a virtual method in the Publisher class to fire the event. Name it 'OnChosenName()'. For example:

       virtual protected OnLogicChanged(LogicChangedEventArgs theChange)
      {
  	if (LogicChangedEvent != null)
     	    LogicChangedEvent(this, theChange);
      }
   
Subclasses of the Publisher class should not register a delegate with the event. Instead, they should override their base class's OnChosenName()  method to receive notification of the event's firing. The overriding method must call its base classâ€™s base.OnChosenName() method. 

This is what Microsoft recommends.

5.  If a Subscriber class wants to be notified when a Publisher class's event fires (and the Subscriber is not a subclass of the Publisher firing the event), it should define a handler method called 'ChosenNameCallback()' with a signature that matches that of the ChosenName delegate. For Example:

    protected virtual void LogicChangedCallback(object source,  LogicChangedEventArgs theChange)

This method should be virtual if subclasses of the Subscriber need to be notified of the event. Subclasses should override ChosenNameCallback()' to be notified when the other class's event fires. The overriding method must call its base class 'base.ChosenNameCallback()' method.

This is not what Microsoft does, at least as far as intellisense is concerned. You may use the intellisense default, but you are encouraged to change the callback function name, especially when creating callback methods for Reporting (as opposed to Microsoft Forms) events. You are also encouraged to change the access modifiers from â€œprivate to â€œprotected virtualâ€? if your subclasses might be interested in the event.
                
6.  Subscriber objects should hook the event using standard 'Publisher.ChosenName += new ChosenNameEventHandler(ChosenNameCallback);' syntax. They should unhook the event using ''Publisher.ChosenName -= new ChosenNameEventHandler(ChosenNameCallback);' syntax. For example:

    aLogic.LogicChanged += new LogicChangedEventHandler(LogicChangedCallback);
    aLogic.LogicChanged -= new LogicChangedEventHandler(LogicChangedCallback);

Note that this delegate will keep the Subscriber from being garbage collected as long as itâ€™s registered with the Publisher. The Subscriber must unhook its delegate when it no longer needs to be notified of Publisher events. Often, this is when the Subscribers Dispose() method is invoked.
</text>
  <last_update>2007-10-04T00:52:31.7240806Z</last_update>
  <source_title />
  <source_link />
  <comments />
  <references />
  <attachments />
</Content>